package com.google.code.afreechartapp.iris;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.afree.chart.AFreeChart;
import org.afree.chart.ChartFactory;
import org.afree.chart.plot.PlotOrientation;
import org.afree.data.xy.XYDataset;
import org.afree.data.xy.XYSeries;
import org.afree.data.xy.XYSeriesCollection;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;
import com.google.code.afreechartapp.iris.R;

public class IRISBase extends Activity {
    
    public enum AxisType {
        PetalWidth,
        PetalLength,
        SepalWidth,
        SepalLength
    }

    protected ArrayList<Iris> mIrisDatas = new ArrayList<Iris>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        try {
            readIrisData();
        } catch (IOException e) {
            Toast.makeText(this, "IOException in readIrisData().", Toast.LENGTH_LONG).show();
        }
    }
    
    protected void showAbout() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.about_label);
        builder.setMessage(R.string.about);
        builder.setPositiveButton(R.string.ok, new android.content.DialogInterface.OnClickListener() {
            public void onClick(android.content.DialogInterface dialog,int whichButton) {
                setResult(RESULT_OK);
            }
        });
        builder.create();
        builder.show();

    }

    protected AFreeChart createChart(int domainType, int rangeType) {
         return ChartFactory.createScatterPlot(
             getString(R.string.app_name),
             getLabel(domainType),
             getLabel(rangeType),
             createDataset(domainType, rangeType),
             PlotOrientation.HORIZONTAL,
             true,
             false,
             false);
     
    }
    
    protected XYDataset createDataset(int domainType, int rangeType) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        XYSeries setosa = new XYSeries(getString(R.string.iris_type_setosa));
        XYSeries versicolor = new XYSeries(getString(R.string.iris_type_versicolor));
        XYSeries verginica = new XYSeries(getString(R.string.iris_type_virginica));
        
        for (Iris iris : mIrisDatas) {
            switch (iris.speciesNo) {
                case Iris.SETOSA :
                    setosa.add(getAxisValue(domainType, iris), getAxisValue(rangeType, iris));
                    break;
                case Iris.VERSICOLOR:
                    versicolor.add(getAxisValue(domainType, iris), getAxisValue(rangeType, iris));
                    break;
                case Iris.VERGINICA :
                    verginica.add(getAxisValue(domainType, iris), getAxisValue(rangeType, iris));
                    break;
                default :
                    continue; // TODO : create and throw exception.
            }
        }
        
        dataset.addSeries(setosa);
        dataset.addSeries(versicolor);
        dataset.addSeries(verginica);
        
        return dataset;
    }
    
    protected static double getAxisValue(int type, Iris iris) {
        if (type == AxisType.PetalWidth.ordinal()) {
            return iris.petalWidth;
        } else if (type == AxisType.PetalLength.ordinal()) {
            return iris.petalLength;
        } else if (type == AxisType.SepalWidth.ordinal()) {
            return iris.sepalWidth;
        } else if (type == AxisType.SepalLength.ordinal()) {
            return iris.sepalLength;
        } else {
            throw new IllegalArgumentException();
        }
    }
    
    protected String getLabel(int type) {
        if (type == AxisType.PetalWidth.ordinal()) {
            return getString(R.string.axis_type_petal_width);
        } else if (type == AxisType.PetalLength.ordinal()) {
            return getString(R.string.axis_type_petal_length);
        } else if (type == AxisType.SepalWidth.ordinal()) {
            return getString(R.string.axis_type_sepal_width);
        } else if (type == AxisType.SepalLength.ordinal()) {
            return getString(R.string.axis_type_sepal_length);
        } else {
            throw new IllegalArgumentException();
        }
    }
    
    private void readIrisData() throws IOException {
        // open iris data file
        BufferedReader br =
            new BufferedReader(
                    new InputStreamReader(
                            getResources().openRawResource(R.raw.fishersiris)));
        
        br.readLine(); // first line is not data
        String line = br.readLine();
        while (line != null) {
            String[] splitedLine = line.split(",");
            mIrisDatas.add(
                    new Iris(
                            Integer.parseInt(splitedLine[0]),
//                            splitedLine[1],
                            Double.parseDouble(splitedLine[2]),
                            Double.parseDouble(splitedLine[3]),
                            Double.parseDouble(splitedLine[4]),
                            Double.parseDouble(splitedLine[5])));
            line = br.readLine();
        }
    }
    
    protected class Iris {
        public static final int SETOSA = 1;
        public static final int VERSICOLOR = 2;
        public static final int VERGINICA = 3;
        
        public int speciesNo;
//        public String speciesName;
        public double petalWidth;
        public double petalLength;
        public double sepalWidth;
        public double sepalLength;
        
        public Iris(int speciesNo, /*String speciesName,*/
                double petalWidth, double petalLength,
                double sepalWidth, double sepalLength) {
            this.speciesNo = speciesNo;
//            this.speciesName = speciesName;
            this.petalWidth = petalWidth;
            this.petalLength = petalLength;
            this.sepalWidth = sepalWidth;
            this.sepalLength = sepalLength;
        }
    }
}
